package com.x86.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.x86.domain.*;
import com.x86.domain.Record;
import com.x86.service.*;
import com.x86.util.ResponseResult;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.Random;

/**
 * @author 风亦未止
 * @date 2022/3/24 19:20
 */
@Slf4j
@RestController
@RequestMapping("pre")
@Api(tags = "客户机数据操作")
public class PreController {

//    @Autowired
//    public PreDataService preDataService;
//    @Autowired
//    public RecordService recordService;
    @Autowired
    public ComputerService computerService;
    @Autowired
    public BatchService batchService;
    @Autowired
    public OperatorService operatorService;
    @Autowired
    public DiskSmartService diskSmartService;
    @Autowired
    public CDService cdService;
    @Autowired
    public MemoryService memoryService;
    @Autowired
    public CMService cmService;
    @Autowired
    public CTestService cTestService;
    @Autowired
    public CpuTestService cpuTestService;

    @PostMapping("/sava")
    @ApiOperation("保存前测数据")
    public ResponseResult saveData(@RequestBody ComputerAndDiskSmart computerAndDiskSmart){
        //查找是否存在此操作员
        Operator operator = operatorService.getById(computerAndDiskSmart.getComputer().getOperatorid());
        if (ObjectUtil.isNull(operator)) return ResponseResult.failure("该操作员id不存在！");
        //是否存在批次，
        Integer lastId = batchService.getLastId();
        Batch batch = batchService.getById(lastId);
        if (ObjectUtil.isNull(batch))return ResponseResult.failure("请先创建批次！");
        if (batch.getQuantity()<= batch.getAlready())return  ResponseResult.failure("该批次检测完成，请创建新的批次！");
        //判断数据库是否有此台机器，有则更新数据，无则保存数据
        Computer byId = computerService.getById(computerAndDiskSmart.getComputer().getCid());
        boolean isSuccess =false;
        if (ObjectUtil.isNotNull(byId)){
            isSuccess = computerService.updateById(computerAndDiskSmart.getComputer());
        }else {
            //存数据
            isSuccess = computerService.save(computerAndDiskSmart.getComputer());
             batchService.already(computerAndDiskSmart.getComputer().getCid());
        }
        DiskSmart diskSmart = diskSmartService.getById(computerAndDiskSmart.getDiskSmart().getDiskid());
        boolean isSuccessD =false;
        if (ObjectUtil.isNotNull(diskSmart)){
            isSuccessD=diskSmartService.updateById(computerAndDiskSmart.getDiskSmart());
        }else {
            isSuccessD=diskSmartService.save(computerAndDiskSmart.getDiskSmart());
            CD cd = new CD();
            cd.setConputerid(computerAndDiskSmart.getComputer().getCid());
            cd.setDiskid(computerAndDiskSmart.getDiskSmart().getDiskid());
            cdService.save(cd);
        }
        Memory memory = memoryService.getById(computerAndDiskSmart.getMemory().getMemId());
        boolean isSuccessM=false;
        if(ObjectUtil.isNotNull(memory)){
            isSuccessM=memoryService.updateById(computerAndDiskSmart.getMemory());
        }else {
            isSuccessM=memoryService.save(computerAndDiskSmart.getMemory());
            CM cm = new CM();
            cm.setComputerId(computerAndDiskSmart.getComputer().getCid());
            cm.setMemId(computerAndDiskSmart.getMemory().getMemId());
            cmService.save(cm);
        }
        if (isSuccess&&isSuccessD&&isSuccessM){
            return  ResponseResult.success("成功！");
        }
        return  ResponseResult.failure("失败！");
    }

    @ApiOperation(value = "批次录入")
    @PostMapping("/batchSava")
    public ResponseResult savaBatch(@RequestBody BatchVO batchVO){
        Batch batch = new Batch();
        batch.setDate(DateUtil.now());
        batch.setQuantity(batchVO.getQuantity());
        //默认已经检测0;
        batch.setAlready(0);
        batch.setName(batchVO.getName());
        batch.setFilePath(batchVO.getFilePath());
        boolean save = batchService.save(batch);
        if (save){
            return  ResponseResult.success("成功！");
        }
        return  ResponseResult.failure("失败！");
    }

    @ApiOperation(value = "后测数据保存")
    @PostMapping("/test")
    public ResponseResult savaTest(@RequestBody TestVO testVO){
        String cid = testVO.getCid();
        Computer computer = computerService.getById(cid);
        if (ObjectUtil.isNull(computer))return ResponseResult.failure("该机器不存在！");
        //查询机器后测数据，有则更新无则插入
        CTest cTest = cTestService.getById(cid);
        if (ObjectUtil.isNull(cTest)){
            while (true){
                int randomId = new Random().nextInt(100000000);
                CpuTest cpuTest = cpuTestService.getById(randomId);
                if (ObjectUtil.isNull(cpuTest)){
                    CpuTest newCpuTest = new CpuTest();
                    newCpuTest.setTestId(randomId);
                    newCpuTest.setCpuTemperature(testVO.getCpuTemperature());
                    newCpuTest.setCpuFanSpeed(testVO.getCpuSpeed());
                    boolean newCpuTestSave = cpuTestService.save(newCpuTest);
                    CTest newCTest = new CTest();
                    newCTest.setCpuPressureTestId(randomId);
                    newCTest.setCid(cid);
                    newCTest.setDiskPressureTest(testVO.getDiskPress());
                    newCTest.setMemoryPressureTest(testVO.getMemoryPress());
                    boolean newCTestSave = cTestService.save(newCTest);
                    if(newCpuTestSave&&newCTestSave){
                        return ResponseResult.success("成功！");
                    }
                }
            }
        }
        cTest.setMemoryPressureTest(testVO.getMemoryPress());
        cTest.setDiskPressureTest(testVO.getDiskPress());
        CpuTest cpu = cpuTestService.getById(cTest.getCpuPressureTestId());
        cpu.setCpuFanSpeed(testVO.getCpuSpeed());
        cpu.setCpuTemperature(testVO.getCpuTemperature());
        boolean b = cpuTestService.updateById(cpu);
        boolean b1 = cTestService.updateById(cTest);
        if (b1&&b){
            return ResponseResult.success("成功！");
        }
        return ResponseResult.failure("失败！");
    }

    @ApiOperation(value = "查询操作员")
    @GetMapping("/operator/{id}")
    public ResponseResult getOp(@PathVariable("id") @ApiParam("操作员id") String operatorid){
        Operator byId = operatorService.getById(operatorid);
        if (ObjectUtil.isNull(byId)){
            return  ResponseResult.failure("操作员id不存在！");
        }
        return  ResponseResult.success(byId.getName());
    }




}
